package com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.StudentList;

import android.content.DialogInterface;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.tnot.R;
import com.example.tnot.Teacher.BaseFragment.BaseBackFragment;

public class StudentDetailFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    View view;
    ImageView back;
    Button btn_confirm;
    ImageView iv_icon;
    TextView et_name,et_ID,et_school,et_class,et_teacher,et_phone,et_grade;
    private boolean isDataChanged = false;

    public StudentDetailFragment() {
    }

    public static StudentDetailFragment newInstance(String param1, String param2) {
        StudentDetailFragment fragment = new StudentDetailFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_student_detail, container, false);
        InitView();
//        addTextWatchers();
        back = view.findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    NavController navController = Navigation.findNavController(view);
                    navController.navigateUp();
            }
        });
        return view;
    }

    private void InitView() {
        // 初始化 EditText 控件
        et_grade= view.findViewById(R.id.et_score);
        et_name = view.findViewById(R.id.et_name);
        et_ID = view.findViewById(R.id.et_ID);
        et_school = view.findViewById(R.id.et_school);
        et_class = view.findViewById(R.id.et_class);
        et_teacher = view.findViewById(R.id.et_teacher);
        et_phone = view.findViewById(R.id.et_phone);
        iv_icon = view.findViewById(R.id.iv_icon);

        if (getArguments() != null) {
            et_name.setText(getArguments().getString("name", ""));
            et_ID.setText(getArguments().getString("ID", ""));
            et_class.setText(getArguments().getString("class", ""));
            //设置学校名称
//            et_school.setText(getArguments().getString("score", ""));
            et_teacher.setText(getArguments().getString("CourseTeacher", ""));
            et_grade.setText(getArguments().getString("score", ""));
            et_phone.setText(getArguments().getString("telephone", ""));
            String  URL=getArguments().getString("icon","");
            RequestOptions options = new RequestOptions()
                    .placeholder(R.drawable.example_face)
                    .error(R.drawable.example_face)
                    .transform(new CenterCrop(), new RoundedCorners(8));

//

            Glide.with(getContext())
                    .load(URL)
                    .apply(options)
                    .into(iv_icon);
            //iv_icon.setImageResource(getArguments().getInt("icon", R.drawable.ic_launcher_background));
        }
    }
    private void showExitConfirmationDialog() {
        new AlertDialog.Builder(getContext())
                .setTitle("退出应用")
                .setMessage("确定要退出修改吗？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        NavController navController = Navigation.findNavController(view);
                        navController.navigateUp();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setCancelable(true)
                .show();
    }

}